package threads;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestTaobaoThread2s {
    private static TestTaobaoThread2s t = new TestTaobaoThread2s();
    private volatile List<String> list = Collections.synchronizedList(new ArrayList<>());

    public void add(String o) {
        list.add(o);
    }

    public int size() {
        return list.size();
    }

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <10; i++) {
                    t.add(String.valueOf(i));
                    System.out.println("add "+i);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"t1").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
               while (true){
                   if(t.size() == 5){
                       break;
                   }
               }
                System.out.println("t2 end.");
            }
        },"t2").start();
    }
}
